<template>
  <div class="container">
    <!-- form查询 -->
    <div class="queryForm" style="margin-top: 20px;margin-left: 15px;">
      <el-form ref="form" :model="searchObj" :inline="true" class="queryForm">
        <el-form-item label="医院名称" prop="hosname">
          <el-input v-model="searchObj.hosname" placeholder="请输入医院名称" />
        </el-form-item>
        <el-form-item label="医院编号" prop="hoscode">
          <el-input v-model="searchObj.hoscode" placeholder="请输入医院编号" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getHospSetList()">查询</el-button>
          <el-button type="danger" @click="ResetTable()">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div style="margin-bottom: 20px;margin-left: 15px;">
      <el-button type="danger" size="mini" @click="batchRows()">批量删除</el-button>
    </div>
    <!-- table -->
    <el-table :data="list" stripe style="width: 100%" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="index" width="50" label="序号" align="center" />
      <el-table-column prop="hosname" label="医院名称" align="center" />
      <el-table-column prop="hoscode" label="医院编号" align="center" />
      <el-table-column prop="apiUrl" label="api基础路径" width="200" align="center" />
      <el-table-column prop="contactsName" label="联系人姓名" align="center" />
      <el-table-column prop="contactsPhone" label="联系人手机" align="center" />
      <el-table-column label="状态" width="80" align="center">
        <template slot-scope="scope">
          {{ scope.row.status === 1 ? '可用' : '不可用' }}
        </template>
      </el-table-column>
      <el-table-column label="锁定/解除" wdith="250" align="center">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="0"
            @change="lockHospSet(scope.row.id, scope.row.status)"
          />
        </template>
      </el-table-column>
      <el-table-column label="操作" wdith="250" align="center">
        <template slot-scope="scope">
          <router-link :to="'/hospSet/edit/'+ scope.row.id">
            <el-button type="primary" size="small">编辑</el-button>
          </router-link>
          <el-button type="danger" size="small" @click="removeDataById(scope.row.id)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :current-page="current"
      :page-size="limit"
      :total="total"
      style="padding:30px 0; text-align: center;"
      layeout="total, prev, pager, next, jumper"
      @current-change="getHospSetList"
    />
  </div>
</template>

<script>
import hospSet from '@/api/hosp/hospSet'
export default {
  name: 'List',
  data() {
    return {
      current: 1,
      limit: 10,
      searchObj: {},
      columns: [
        {
          type: 'index',
          label: '序号',
          prop: 'id',
          width: 50
        },
        {
          prop: 'hosname',
          label: '医院名称'
        },
        {
          prop: 'hoscode',
          label: '医院编号'
        },
        {
          prop: 'apiUrl',
          label: 'api基础路径',
          width: 200
        },
        {
          prop: 'contactsName',
          label: '联系人姓名'
        },
        {
          prop: 'contactsPhone',
          label: '联系人手机'
        },
        {
          prop: 'status',
          label: '状态',
          width: 80
        }
      ],
      list: [],
      total: 0,
      multipleSelection: [],
      lock: true,
      form: {
        hosname: '',
        hoscode: ''
      }
    }
  },
  created() {
    this.getHospSetList()
  },
  methods: {
    getHospSetList(page = 1) {
      this.current = page
      hospSet.getHospSetList(this.current, this.limit, this.searchObj)
        .then((res) => {
          console.log(res)
          this.list = res.data.records
          this.total = res.data.total
        }).catch(error => {
          console.log(error)
        })
    },
    lockHospSet(id, status) {
      hospSet.lockHospSet(id, status).then((res) => {
        this.$message({
          type: 'success',
          message: '操作成功!'
        })
        this.getHospSetList(this.current)
      }).catch((error) => {
        console.log(error)
      })
    },
    ResetTable() {
      this.$refs.form.resetFields()
    },
    handleSelectionChange(selection) {
      this.multipleSelection = selection
    },
    batchRows() {
      if (this.multipleSelection.length === 0) {
        this.$message({
          type: 'warning',
          message: '请选择要删除的记录'
        })
        return
      }
      this.$confirm('此操作将永久删除医院的设置信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const idsList = []
        this.multipleSelection.map((item) => {
          idsList.push(item.id)
        })
        hospSet.batchRemoveHospSet(idsList).then((res) => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getHospSetList()
        }).catch((error) => {
          console.log(error)
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    removeDataById(id) {
      this.$confirm('此操作将永久删除医院的设置信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        hospSet.deleteHospSet(id).then((res) => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getHospSetList()
        }).catch((error) => {
          console.log(error)
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>

<style lang="less">
</style>
